
function get_appropriate_ws_url(extra_url)
{
	var pcol;
	var u = document.URL;

	/*
	 * We open the websocket encrypted if this page came on an
	 * https:// url itself, otherwise unencrypted
	 */

	if (u.substring(0, 5) === "https") {
		pcol = "wss://";
		u = u.substr(8);
	} else {
		pcol = "ws://";
		if (u.substring(0, 4) === "http")
			u = u.substr(7);
	}

	u = u.split("/");

	/* + "/xxx" bit is for IE10 workaround */

	return pcol + u[0] + "/" + extra_url;
}

function new_ws(urlpath, protocol)
{
	return new WebSocket(urlpath, protocol);
}

document.addEventListener("DOMContentLoaded", function() {
	
	var ws = new_ws(get_appropriate_ws_url(""), "lws-minimal");
	try {
		ws.onopen = function() {
			document.getElementById("m").disabled = 0;
			document.getElementById("b").disabled = 0;
			document.getElementById("bleadv").disabled = 0;
			document.getElementById("blescan").disabled = 0;
			document.getElementById("bleota").disabled = 0;
		};
	
		ws.onmessage =function got_packet(msg) {
			switch (msg.data[0]) {
				case 'o':
					document.getElementById("r").value =
						document.getElementById("r").value + msg.data;
					break;
				case 'e':
					document.getElementById("r2").value =
						document.getElementById("r2").value + msg.data;
					break;
				case 's':
					document.getElementById("syslog").value =
						document.getElementById("syslog").value + msg.data;
				case 'F':
					if(msg.data[1] == 's')
					{
						document.getElementById("Fsend").value =
						document.getElementById("Fsend").value + "上传成功";
					}
					break;
				default:
					break;
			}
			document.getElementById("r").scrollTop =
				document.getElementById("r").scrollHeight;
			document.getElementById("r2").scrollTop =
				document.getElementById("r2").scrollHeight;
			document.getElementById("syslog").scrollTop =
				document.getElementById("syslog").scrollHeight;
		};
	
		ws.onclose = function(){
			document.getElementById("m").disabled = 1;
			document.getElementById("b").disabled = 1;
			document.getElementById("bleadv").disabled = 1;
			document.getElementById("blescan").disabled = 1;
			document.getElementById("bleota").disabled = 1;
		};
	} catch(exception) {
		alert("<p>Error " + exception);  
	}

	function sendmsgcmd(cmd)
	{
		switch (cmd) {
			case 1:
				ws.send("bleadv:" + document.getElementById("advtime").value + "s\n");
				if(document.getElementById("advtime").value = "".length == 0) document.getElementById("advtime").value = "0";
				break;
			case 2:
				ws.send("blescan\n");
				break;
			case 3:
				ws.send("bleota\n");
				breakl;
			case 4:
				ws.send(document.getElementById("m").value);
				document.getElementById("m").value = "";
				break;
			default:
				break;
		}
	}
	
	document.getElementById("b").addEventListener("click", function () {
		sendmsgcmd(4);
	}, true);
	document.getElementById("bleadv").addEventListener("click", function () {
		sendmsgcmd(1);
	}, true);
	document.getElementById("blescan").addEventListener("click", function () {
		sendmsgcmd(2);
	}, true);
	document.getElementById("bleota").addEventListener("click", function () {
		sendmsgcmd(3);
	}, true);

}, false);

